//
//  UIButton + Extension.swift
//  Weibo10
//
//  Created by 吕昱坪 on 15/12/7.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

extension UIButton {
    
    /// `便利`构造函数
      //遍历构造函数通过self.init()把对象创建起来  便利构造函数是不需要返回值的函数执行完对象就创建完了
    /**
    - parameter imageName:     图像名称
    - parameter backImageName: 背景图像名称
    
    - returns: UIButton
    */
    convenience init(imageName: String, backImageName: String?) {
      
        self.init()
        
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
       
        if let backImageName = backImageName{
        setBackgroundImage(UIImage(named: backImageName), forState: UIControlState.Normal)
        setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: UIControlState.Highlighted)
        }
        
        // 会根据背景图片的大小调整尺寸
        sizeToFit()
    }
    
    /**
     便利构造函数
     
     - parameter title:     title
     - parameter color:     color
     - parameter backImageName: 背景图像
     
     - returns: UIButton
     */
    convenience init(title: String, color: UIColor, backImageName: String){
        self.init()
        
       setTitle(title, forState: .Normal)
       setTitleColor(color, forState: .Normal)
       setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        
        sizeToFit()
}
    
    ///底部视图按钮的构造函数
    ///
    /// - parameter title:     标题
    /// - parameter color:     颜色
    /// - parameter fontSize:  字体大小
    /// - parameter imageName: 图片名称
    /// - parameter backColor: 背景颜色(默认为nil)
    /// - returns: UIButton
    convenience init(title: String, color: UIColor, fontSize: CGFloat, imageName: String?, backColor: UIColor? = nil ){
        self.init()
        
        setTitle(title, forState: .Normal)
        setTitleColor(color, forState: .Normal)
        
        if let imageName = imageName {
        setImage(UIImage(named: imageName), forState: .Normal)
        }
        
        //设置背景颜色
        backgroundColor = backColor
        
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        sizeToFit()
    }
}









